<template>
  <a-modal
    :title='title'
    :width='560'
    :visible='visible'
    :isLoading='isLoading'
    :maskClosable='false'
    @ok='handleSubmit'
    @cancel='handleCancel'
  >
    <a-spin :spinning='isLoading'>
      <a-form :form='form'>
        <a-form-item label='付款状态' :labelCol='labelCol' :wrapperCol='wrapperCol'>
          <a-radio-group
            v-decorator="['pay_status', {initialValue: 10, rules: [{ required: true, message: '选择付款状态' }]}]"
          >
            <a-radio :value='10'>未付款</a-radio>
            <a-radio :value='20'>已付款</a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item
          label='支付方式'
          :labelCol='labelCol'
          :wrapperCol='wrapperCol'
          extra='切换支付状态会重复扣除用户余额，请谨慎操作'
        >
          <a-radio-group
            v-decorator="['pay_type', {initialValue: 10, rules: [{ required: true, message: '选择服务类型' }]}]"
          >
            <a-radio :value='10'>余额支付</a-radio>
            <a-radio :value='30'>线下支付</a-radio>
          </a-radio-group>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import _ from 'lodash'
import * as Api from '@/api/order/event'

export default {
  data() {
    return {
      // 对话框标题
      title: '修改订单支付状态',
      // 标签布局属性
      labelCol: { span: 7 },
      // 输入框布局属性
      wrapperCol: { span: 13 },
      // modal(对话框)是否可见
      visible: false,
      // modal(对话框)确定按钮 loading
      isLoading: false,
      // 当前表单元素
      form: this.$form.createForm(this),
      // 当前记录
      record: {}
    }
  },
  created() {
  },
  methods: {

    /**
     * 显示对话框
     */
    show(record) {
      // 显示窗口
      this.visible = true
      // 当前记录
      this.record = record
      // 设置默认值
      this.setFieldsValue()
    },

    // 设置默认值
    setFieldsValue() {
      const { record, $nextTick, form: { setFieldsValue } } = this
      $nextTick(() => {
        setFieldsValue({
          pay_type: Number(record.pay_type),
          pay_status: Number(record.pay_status),
        })
      })
    },

    // 确认按钮
    handleSubmit(e) {
      e.preventDefault()
      // 表单验证
      const { form: { validateFields } } = this
      validateFields((errors, values) => {
        // 提交到后端api
        !errors && this.onFormSubmit(values)
      })
    },

    /**
     * 关闭对话框事件
     */
    handleCancel() {
      this.visible = false
      this.form.resetFields()
    },

    /**
     * 提交到后端api
     */
    onFormSubmit(values) {
      this.isLoading = true
      Api.updateStatus({ orderId: this.record.order_id, form: values })
        .then((result) => {
          // 显示成功
          this.$message.success(result.message, 1.5)
          // 关闭对话框事件
          this.handleCancel()
          // 通知父端组件提交完成了
          this.$emit('handleSubmit', values)
        })
        .finally(() => {
          this.isLoading = false
        })
    }

  }
}
</script>
